const co = require('co');
const moment = require("moment");
const uuid = require('uuid');
const security = require('../providers/security');

const assets = require("./assets.json");
const db = require("../providers/dataAccess").assets;
var request = require("request");
var fs = require("fs");


var httpRequest = function (opt,httpOnly) {
    return function (callback) {
		opt.headers = opt.headers || {};
		opt.headers["accept-encoding"] = "gzip,deflate";

		var http = httpOnly ? require('http') : require('https');
        var req = http.request(opt,function(res){
            var resChunks = [];
            res.on('data', function (chunk) {
                resChunks.push(chunk);
            });
            res.on('end',function () {
                var buffer = Buffer.concat(resChunks);
                  var encoding = res.headers['content-encoding'];
                  if (encoding == 'gzip') {
                    zlib.gunzip(buffer, function(err, decoded) {
                      callback(err, decoded && decoded.toString());
                    });
                  } else if (encoding == 'deflate') {
                    zlib.inflate(buffer, function(err, decoded) {
                      callback(err, decoded && decoded.toString());
				  	});
                  } else {
                    callback(null, buffer.toString());
                  }
            });
        });
        req.on('error',function(err){
			console.log('request.error:' + err.message);
            callback(err,null);
        });
		if (opt.data){
            console.log(opt.data);
			req.write(opt.data);
		}
        req.end();
    }
}

var parseImages = function* () {
    var getPageResult = function *(pageIndex) {
        var opt = {
            host: "www.zhichiwangluo.com",
            path: "/index.php?r=pc/UserTag/getImgList&tag_id=0&page="+ pageIndex +"&page_size=40&user=0",
            method: "GET",
            headers: {
                "Cookie":"gr_user_id=31cab9b7-26df-461a-a986-7c087ab5ba80; PHPSESSID=g5mngqntk2upbp36f745a7i4a7; CookieId=59e054fab9b318.69849345; notice=1507717582; Hm_lvt_dbc2f06ada4968aa325cbb4e8932d648=1507874183; Hm_lpvt_dbc2f06ada4968aa325cbb4e8932d648=1507903449; pgv_pvi=7629655688; pgv_info=ssi=s8291610202; gr_session_id_9fd21c28eb55d8a5=1ed5759d-27bf-4f6b-9b10-0613a9e68e99"
            }
        };
        const res = yield httpRequest(opt, true);
        return res;
    }
    var pIndex = 1;
    var isMore = true;

    var szSql = "INSERT INTO sys_images(tagId, fileName, fileExt, original) VALUES(?,?,?,?);"
    do{
        console.log("Parse Page: " + pIndex);
        var pageJson = yield getPageResult(pIndex++);
        pageJson = JSON.parse(pageJson);
        for(var i = 0; i< pageJson.data.length; i++){
            var data = pageJson.data[i];

            var original = data.img_original;
            var tagId = parseInt(data.user_tag_id);

            var fExt = original.slice(-4);
            if (fExt[0] != ".") fExt = '';
            var fileName = security.md5(original) + fExt;
            request(original).pipe(fs.createWriteStream('/Users/HanBao/HongDao/HongDao.IRS/irs-api.hongdaoit.com/tools/assets/'+fileName));
            console.log(original + " => " + fileName);

            yield db.query(szSql,[tagId, fileName, fExt, original]);
        }
        isMore = pageJson.is_more;
    }while(isMore)

}

// co(function *() {
//     console.log(assets.data.length);
//     var szSqlTag = "INSERT INTO sys_image_tags(tagId, parentId, tagName) VALUES(?, ?, ?)";
//     assets.data.forEach(function(item) {
//         co(function *() {
//             var parentTagId = parseInt(item.cate_id);
//             yield db.query(szSqlTag,[parentTagId, 0, item.name]);
//
//             item.cate.forEach(function(subItem){
//                 co(function *() {
//                     var tagId = parseInt(subItem.cate_id);
//                     yield db.query(szSqlTag,[tagId, parentTagId, subItem.name]);
//                 });
//
//             });
//         });
//
//     })
// });

co (function *() {
    yield parseImages();
    console.log("Finished.....");
})
